<?php
/**
 * @author Fernando Palomo Garcia  fernando@kernelius.com
 * @license Apache license 2.0
 * @version 0.1
 * @copyright 2009
 */
class Kernelius_Logger{

    /**
     * log file has been initialized
     * @var bool
     */
    protected static $logCreated=FALSE;

    /**
     *
     * @var string logfile path
     */
    protected static $logFile;


    /**
     * default log file
     */
    const DEFAULT_LOG_FILE="/var/log/app.log";


    /**
     * init file
     * @param string $logFile
     * @return bool
     */
    protected static function initLog($logFile=NULL){
       if(self::$logCreated ){
           return;
       }
       if(!$logFile){
           self::$logFile=self::DEFAULT_LOG_FILE;
       }
       touch(self::$logFile);
       self::$logCreated=TRUE;
    }


    /**
     * print message to log
     * @param mixed $message
     * @param string $errorLevel
     * @param string $logFile
     */
    public static function log( $message, $errorLevel="DEBUG", $logFile=NULL){
        self::initLog($logFile);
        if(is_array($message)){
            $message= print_r($message,true);
        }
        file_put_contents( self::$logFile, date("Y-m-d H:i:s")." [$errorLevel] => ". $message."\n", FILE_APPEND);
    }


    /**
     * next messages written to this file
     * @param string $path
     */
    public static function setLogFile($path){
        self::initLog($path);
    }

    public static function exception($message, $code=0){
        self::log($message);
        throw new exception ($message, $code);
    }

}
